var bannerAd = null;
var videoAd = null
// const Wechat = require('ym-Wechat')
const Events = require('ym-Events')
const Http = require('ym-Http')
const DataMgr = require('ym-Data')

function onVideoError(){
    cc.game.emit(cc.game.EVENT_SHOW)
    if(!this.videoOptions.autoShare){
        this.videoOptions.fail && this.videoOptions.fail(1)
    } else {
        Wechat.share(this.videoOptions);
    }
}
function onVideoClose(res){
    cc.game.emit(cc.game.EVENT_SHOW)
    if(res.isEnded){
        Http.loginGet({
            router:'channelEvent.add',
            data :{eventName:"观看视频成功"},
            cb:function(){}
        })
        if(DataMgr.getData('isNewUser')){
            Http.loginGet({
                router:'channelEvent.add',
                data :{eventName:"新用户观看视频成功"},
                cb:function(){}
            })
        }
        this.videoOptions.success && this.videoOptions.success()
    } else {
        this.videoOptions.fail && this.videoOptions.fail(2)
    }
}

module.exports = {
    showBanner(aid,x,y,time=0){
        this._bannerx = x;
        this._bannery = y;
        var self = this;
        var systemInfo = wx.getSystemInfoSync();
        var height = systemInfo.screenHeight * 0.35;
        var width = systemInfo.screenWidth;
        if (!!bannerAd) bannerAd.destroy();
        bannerAd = wx.createBannerAd({
            adUnitId: aid,
            style: { left: 0, top: 0, width: width, height: height }
        });
        bannerAd.onResize((res) => {
            if (!bannerAd) return;
            bannerAd.style.top =systemInfo.screenHeight- res.height;
            bannerAd.style.left = 0
            Events.emit('bannershow',res)
        })
        bannerAd.onError((e) => { console.log('onError:', e); });
        bannerAd.show();
        if(time){
            if(this._updateBanner){
                clearTimeout(this._updateBanner);
            }
            this._updateBanner = setTimeout(function(){
                this.showBanner(aid,this._bannerx,this._bannery,time)
            }.bind(this),time*1000)
        }
    },
    moveBanner(x,y){
        this._bannerx = x;
        this._bannery = y
        cc.log('moveBanner:',x,y)
        var systemInfo = wx.getSystemInfoSync();
        var th = y *systemInfo.screenHeight / cc.winSize.height
        bannerAd.style.top = systemInfo.screenHeight-th;
        bannerAd.style.left = x *systemInfo.screenWidth / cc.winSize.width
    },
    destroyBanner(){
        bannerAd && bannerAd.destroy();
        bannerAd = null;
        if(this._updateBanner){
            clearTimeout(this._updateBanner);
        }
        this._updateBanner = null
    },
    showVideo(options){
        if(!options.adUnitId){
            return cc.error('没有unitId')
        }
        var defaultOptions = {
            adUnitId:'', //视频ID
            autoShare: false, //视频失败后，自动转分享
            checkTime:true, //是否检测分享时间
            success:function(){},//成功方法
            fail:function(number){} //fail
        }
        this.videoOptions = Object.assign(defaultOptions,options);
        if(!this._onVideoError){
            this._onVideoError = onVideoError.bind(this)
        }
        if(!this._onVideoClose){
            this._onVideoClose = onVideoClose.bind(this)
        }
        if(!!videoAd){
            videoAd.offError(this._onVideoError);
            videoAd.offClose(this._onVideoClose);
        }
        videoAd = wx.createRewardedVideoAd({ adUnitId: this.videoOptions.adUnitId });
        videoAd.load().then(() =>{
            cc.log('广告准备完成');
            cc.game.emit(cc.game.EVENT_HIDE)
            videoAd.show().then(()=>{
                cc.log('广告开始显示');
            });
        });
        videoAd.onError(this._onVideoError);
        videoAd.onClose(this._onVideoClose);
    },
    isShowing(){
        return bannerAd;
    },
    getOptions(){
        return {
            bannerPos : cc.v2(this._bannerx,this._bannery)
        }
    }
}